<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class RegisterNotification extends Notification
{
    use Queueable;
    protected $user;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($user)
    {
//        dd(123);
//        因为下面要去user模型中的email字段
        $this->user = $user;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
//        $code = str_random(4);
//        随机生成4个数字
        $seed = '0123456789';
        $code = '';
        for ($i=0;$i<4;$i++){
            $num = rand(0,strlen($seed)-1);
            $yzm = $seed[$num];
            $code = $code . $yzm;
        }
//        缓存邮箱和生成的随机验证码
        session(['register'=>['code'=>$code,'email'=>$this->user['email']]]);
        return (new MailMessage)
            ->line('感谢使用期许开发网站,你的验证码是'.$code)
            ->action('Notification Action', url('/'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}
